package mx.com.crowdgine.game;

import java.nio.FloatBuffer;

import javax.media.opengl.GLAutoDrawable;

import mx.com.crowdgine.app.JoglApplication;
import mx.com.crowdgine.assetloader.Asset;
import mx.com.crowdgine.assetloader.OBJModelAsset;
import mx.com.crowdgine.camera.JoglCamera;
import mx.com.crowdgine.display.animation.glsl.GLSLAnimation;

import com.jme3.input.MouseInput;
import com.jogamp.common.nio.Buffers;


public class VarietyPlayground extends JoglApplication{
	private int crowdSize = 10*10;
	private FloatBuffer dataArray []= new FloatBuffer[crowdSize];
	public Asset crowds[] = new Asset[crowdSize];
	
	public float time;
	
	public VarietyPlayground(String frameName, int width, int height){
		super(frameName, width, height);
	}
	
	public static void main(String [] args){
		VarietyPlayground crowdGame =  new VarietyPlayground("Eager to make you smile gameTest" , 640, 480);
		crowdGame.start();
	}
	
	public void initialize(){
		super.initialize();
		addAndSetCamera(new JoglCamera());
		
		crowds[0] = new OBJModelAsset();
	    crowds[0].setAnimation(new GLSLAnimation("Assets/shaders/wobbleVertex.glsl", "Assets/shaders/wobbleFragment.glsl"));
	    crowds[0].setAssetName("Assets/Models/mrT");
	    addAsset(crowds[0]);
	    
	    for(int i = 1; i < crowdSize ; i ++){
	    	crowds[i] = new OBJModelAsset();
	    	crowds[i].setAnimation(new GLSLAnimation("Assets/shaders/sizeVertex.glsl", "Assets/shaders/sizeFragment.glsl"));
	    	crowds[i].setAssetName("Assets/Models/mrT");
	    	addAsset(crowds[i]);
	    }
	}
	
	public void initCrowdPositionData(){
		float posX = -10;
		float posY =  0;
		float posZ =  10;
		float stride = 1;
		float value[] = new float[]{0f,0.0f, 0.0f, 0.0f};
		
		double length = Math.pow(crowdSize, 0.5);
		
		for(int i = 0; i < length ; i++){
			for(int j = 0; j <length; j++){
				dataArray[(int)(j+(i*length))] = Buffers.newDirectFloatBuffer(value);
				dataArray[(int)(j+(i*length))].put(0,posX + (stride *j));
				dataArray[(int)(j+(i*length))].put(1,posY);
				dataArray[(int)(j+(i*length))].put(2,posZ + (stride *i));
			}
			
		}
		
	}
	
	public void init(GLAutoDrawable drawable){
		super.init(drawable);
		initCrowdPositionData();
		System.out.println("Init drawable in Variety Playground");
	}
	
	public void initInput(){
		super.initInput();
		addMouseButtonMapping("Jump", MouseInput.BUTTON_LEFT,MouseInput.BUTTON_RIGHT);
	}
	public void update(GLAutoDrawable drawable){
		super.update(drawable);
		inputManager.update();
		float value[] = new float[]{1.0f,0.0f, 10.0f, 0.0f};
		float light[] = new float[]{1.0f,1.0f,1.0f};
		
		//FloatBuffer data = Buffers.newDirectFloatBuffer(value);
		FloatBuffer lightData = Buffers.newDirectFloatBuffer(light);
		
		crowds[0].getAnimation().setParameter("roll" , time);
		crowds[0].getAnimation().setParameter("LightPos0", new float[]{1.0f,1.0f,1.0f});
		
		for(int i = 1; i<crowdSize; i++){
		
			crowds[i].getAnimation().setParameter("roll" , time);
			//crowds[i].getAnimation().setParameter("LightPos0", new float[]{1.0f,1.0f,1.0f});
		//	crowds[i].getAnimation().setParameter("LightPos0",lightData);
			
		//	crowds[i].getAnimation().setParameter("position", data);
	//		crowds[i].getAnimation().setParameter("position", new float[]{1.0f,0.0f, 10.0f ,0.0f});
			crowds[i].getAnimation().setParameter("position", dataArray[i], 4, 4);
		//	System.out.println("crowds["+i+"]=["+dataArray[i].get(0)+","+dataArray[i].get(1)+","+dataArray[i].get(2)+"]");
			//crowds[i].getAnimation().setDynamicParameter("LightPos0", lightData);
		//	crowds[i].getAnimation().setDynamicParameter("position", data);
			
		}
		
		time+=0.1;
	}
	
	@Override
	protected void updateActionListener(String name, boolean pressed){
		super.updateActionListener(name, pressed);
		if(name=="Jump" && pressed){
			System.out.println("ROFL");
		}
	}
	
	@Override
	protected void updateAnalogListener(String name, float value){
		super.updateAnalogListener(name, value);
		if(name=="MouseLeft"){
			System.out.println("Left! value: "+value);
		}
		if(name=="MouseRight"){
			System.out.println("Right! value: "+value);
		}
		if(name=="MouseUp"){
			System.out.println("Up! value: "+value);
		}
		if(name=="MouseDown"){
			System.out.println("Down! value: "+value);
		}
	}
}
